<template>
  <div class="order-list">
    <order-header title="订单列表">
      <template v-slot:tips>
        <span>
          温馨提示：请谨防钓鱼链接或诈骗电话，了解更多
        </span>
      </template>
    </order-header>
    <div class="wrapper">
      <div class="container">
        <div class="order-box">
          <loading v-if="loading"></loading>
          <div class="order" v-for="(item,index) of list" :key="index">
            <div class="order-title">
              <div class="item-info fl">
                {{item.createTime}}
                <span>|</span>
                {{item.receiverName}}
                <span>|</span>
                订单号：{{item.orderNo}}
                <span>|</span>
                {{item.paymentTypeDesc}}
              </div>
              <div class="item-money fr">
                <span>应付金额：</span>
                <span class="money">{{item.payment}}</span>
                <span>元</span>
              </div>
            </div>
            <div class="order-content clearfix">
              <div class="good-box fl">
                <div class="good-list" v-for="(good, i) in item.orderItemVoList" :key="i">
                  <div class="good-img">
                    <img v-lazy="good.productImage" alt="">
                  </div>
                  <div class="good-name">
                    <div class="p-name">{{good.productName}}</div>
                    <div class="p-money">{{good.currentUnitPrice}} X {{good.quantity}}元</div>
                  </div>
                </div>
              </div>
              <div class="good-state fr" v-if="item.status===10">
                <a href="javascript:;" @click="goPay(item.orderNo)">{{item.statusDesc}}</a>
              </div>
              <div class="good-state fr" v-else >
                <a href="javascript:;">{{item.statusDesc}}</a>
              </div>
            </div>
          </div>
          <!-- 分页器 -->
          <el-pagination
            v-if="true"
            class="pagination"
            background 
            layout="prev, pager, next" 
            :total="total"
            :page-size="pageSize"
            :current-page="pageNum"
            @current-change="handleChange"
          >
          </el-pagination>
          <!-- 按钮分页加载 -->
          <div class="load-more pagination" v-if="false">
            <el-button type="primary" :loading="loading" @click="loadMore" :disabled="btnDisable">
              <span v-if="!btnDisable">加载更多</span>
              <span v-else>暂无更多数据</span>
            </el-button>
          </div>
          <!-- 滚动加载 -->
          <!-- <div
            class="scroll-more"
            v-infinite-scroll="scrollMore"
            infinite-scroll-disabled="busy"
            infinite-scroll-distance="410"
          >
            <img src="/imgs/loading-svg/loading-spinning-bubbles.svg" v-show="loading">
          </div> -->
          <no-data v-if="!loading && !list.length" message="当前暂无提交的订单记录"></no-data>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import OrderHeader from '@/components/OrderHeader'
  import Loading from '@/components/Loading'
  import NoData from '@/components/NoData'
  import { Pagination, Button } from 'element-ui'
  import infiniteScroll from 'vue-infinite-scroll'
  export default {
    name: 'order-list',
    components: {OrderHeader, Loading, NoData, [Pagination.name]: Pagination, [Button.name]: Button},
    directives: {infiniteScroll},
    data() {
      return {
        list: [],
        loading: false,
        pageSize: 2,
        pageNum: 1,
        total: 0,
        pageCount: 0,
        btnDisable: false,
        busy: false
      }
    },
    mounted() {
      this.getOrderList()
    },
    methods: {
      getOrderList() {
        this.loading = true
        this.axios.get(`/orders`, {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize
          }
        })
        .then(res => {
          this.list = this.list.concat(res.list)
          this.loading = false
          this.total = res.total
          this.busy = false
          this.pageCount = Math.floor(this.total / this.pageSize)
        })
        .catch(err => {
          console.error(err);
          this.loading = false
        })
      },
      goPay(orderNo) {
        this.$router.push({
          path:'/order/pay',
          query: {
            orderNo
          }
        })
      },
      handleChange(pageNum) {
        this.pageNum = pageNum
        this.getOrderList()
      },
      loadMore() {
        console.log(this.pageNum, this.pageCount)
        if (this.pageNum <= this.pageCount) {
          this.pageNum ++
          this.getOrderList()
        } else {
          this.btnDisable = true
        }
      },
      // scrollMore () {
      //   this.busy = true,
      //   setTimeout(() => {
      //     this.pageNum ++
      //     this.getList()
      //     this.busy = false;
      //   }, 2000)
      // },
      // // 滚动使用
      // getList() {
      //   this.loading = true
      //   this.axios.get(`/orders`, {
      //     params: {
      //       pageNum: this.pageNum,
      //       pageSize: this.pageSize
      //     }
      //   })
      //   .then(res => {
      //     this.list = this.list.concat(res.list)
      //     this.loading = false
      //     if(!res.hasNextPage) {
      //       console.log('进来')
      //       this.busy = false
      //     } else {
      //       this.busy = true
      //     }
      //   })
      //   .catch(err => {
      //     console.error(err)
      //   })
      // },
    },
  }
</script>

<style lang='scss'>
  @import './../assets/scss/config.scss';
  @import './../assets/scss/mixin.scss';
  .order-list{
    .wrapper{
      background-color:$colorJ;
      padding-top:33px;
      padding-bottom:110px;
      .order-box{
        .order{
          border: 1px solid #C7C7C7;;
          background-color:$colorG;
          margin-bottom:31px;
          &:last-child{
            margin-bottom:0;
          }
          .order-title{
            height: 74px;
            line-height: 74px;
            background-color:$colorK;
            padding:0 43px;
            font-size:16px;
            color:$colorC;
            .item-info{
              span{
                margin:0 9px;
              }
            }
            .money{
              font-size:26px;
              color:$colorB;
            }
          }
          .order-content{
            padding:0 43px;
            .good-box{
              width:88%;
              .good-list{
                display: flex;
                align-items: center;
                height:145px;
                .good-img{
                  width:112px;
                  img{
                    width:100%;
                  }
                }
                .good-name{
                  font-size:20px;
                  color:$colorB;
                }
              }
            }
            .good-state{
              height: 145px;
              line-height: 145px;
              font-size: 20px;
              color:$colorA;
              a{
                color:$colorA;
              }
            }
          }
        }
        .pagination {
          text-align:center;
        }
        .el-pagination.is-background .el-pager li:not(.disabled).active{
          background-color: #FF6600;
        }
        .el-button--primary{
          background-color: #FF6600;
          border-color: #FF6600;
        }
        .el-button.is-disabled {
          background-color: #FF660036;
          border-color: #FF660036;
        }
        .load-more,.scroll-more{
          text-align:center;
        }
      }
    }
  }
</style>
